let istk = false;
function Interceptfun() {
  const whiteList = [
    "/pages/index/index",
    "/pages/my/index",
    "/pages/login/login",
    "/pages/goods/details/index",
    "/pages/index/list",
    "/pages/mine/userAgreement",
    "/pages/mine/userAgreementDeatils",
    "/pages/goods/order_confirm/index",
    "/pages/user_about/index",
    "/pages/mysetting/window/index",
    "/pages/user_about/index?from=sys_user_agree",
    "/pages/user_about/index?from=sys_user_privacy",
  ];

  // 全局页面跳转拦截
  uni.addInterceptor("navigateTo", {
    invoke(args) {
      console.log("拦截器", whiteList.includes(args.url), args.url);
      // return;

      if (
        !uni.getStorageSync("X-Auth-Token") &&
        !whiteList.includes(args.url)
      ) {
        if (
          args.url.indexOf("/pages/goods/details/index") != -1 ||
          args.url.indexOf("/pages/index/list") != -1 ||
          args.url.indexOf("/pages/mine/userAgreementDeatils") != -1 ||
          args.url.indexOf("/pages/goods/order_confirm/index") != -1
        ) {
          return true;
        } else {
          IsLoginfun();
          return false;
        }
      }
      return true;
    },
    success(args) {},
    fail(err) {},
    complete(res) {},
  });

  // 全局页面跳转拦截
  // uni.addInterceptor("switchTab", {
  //   invoke(args) {

  //     if (uni.getStorageSync("Authorization") && islogin.includes(args.url)) {
  //       if (res.cancel && getCurrentPages().length > 1) {
  //         uni.navigateBack();
  //       } else {
  //         uni.redirectTo({
  //           url: "/pages/index/index",
  //         });
  //       }
  //     }
  //     if (
  //       !uni.getStorageSync("Authorization") &&
  //       !whiteList.includes(args.url)
  //     ) {
  //       IsLoginfun();
  //       return false;
  //     }
  //     return true;
  //   },
  //   success(args) {
  //   },
  //   fail(err) {
  //   },
  //   complete(res) {
  //   },
  // });
}

function IsLoginfun() {
  if (!istk) {
    istk = true;
    uni.showModal({
      title: "温馨提示",
      content: "此时此刻需要您登录喔~",
      // showCancel: false,
      confirmText: "去登录",
      cancelText: "再逛会",
      success: (res) => {
        if (res.confirm) {
          uni.navigateTo({
            url: "/pages/login/login",
          });
        }
        if (res.cancel && getCurrentPages().length > 1) {
          uni.navigateBack();
        } else {
          return false;
        }
      },
      complete(res) {
        istk = false;
      },
    });
  }
}
// const IsLoginfun={
//     loginModal:false
// }

export { Interceptfun, IsLoginfun };
